//
//  TB_FMDBManager.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/10/24.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "TB_FMDBManager.h"
#import <FMDB.h>

@interface TB_FMDBManager()
{
    FMDatabaseQueue *Quedb;
    
}

@end

@implementation TB_FMDBManager

//单例写法


+(instancetype)shareDataBase
{
    static TB_FMDBManager *manger=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        manger=[[TB_FMDBManager alloc]init];
        
        [manger CreatDB];//创建数据库
    });
    
    return manger;
}

//创建数据库
-(void)CreatDB
{
    NSString*documentPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    
    NSString*sqlPath=[documentPath stringByAppendingPathComponent:@"SearchHistory.sqlite"];
    
    Quedb=[FMDatabaseQueue databaseQueueWithPath:sqlPath];
    
    [Quedb inDatabase:^(FMDatabase * _Nonnull db) {
       
        BOOL success = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_searchhistory (id INTEGER PRIMARY KEY AUTOINCREMENT, searchkey TEXT, searchdate TEXT);"];
        
        if (success) {
            NSLog(@"创建表成功");
        } else {
            NSLog(@"创建表失败");
        }
    }];
    
}

-(void)inSertHistoryWithModel:(HistoryModel*)model
{
    NSArray *arr=[self queryAllData];
    
    BOOL isExist=NO;
    
    for (HistoryModel *zmodel in arr) {
        
        if ([model.searchkey isEqualToString:zmodel.searchkey]) {
            
            isExist=YES;
            
            break;
        }
        
    }
    
    if (!isExist) {
        
        [Quedb inDatabase:^(FMDatabase * _Nonnull db) {
            
            BOOL success=[db executeUpdate:@"INSERT INTO t_searchhistory (searchkey,searchdate) VALUES (?,?)",model.searchkey,model.searchdate];
            
            if (success) {
                NSLog(@"插入成功");
            } else {
                NSLog(@"插入失败");
            }
            
        }];
    }
  
}

-(void)deleDataWitmModel:(HistoryModel*)model
{
    [Quedb inDatabase:^(FMDatabase * _Nonnull db) {
       
        BOOL success=[db executeUpdate:@"DELETE FROM t_searchhistory WHERE searchkey=? ",model.searchkey];
        
        if (success) {
            NSLog(@"删除成功");
        } else {
            NSLog(@"删除失败");
        }
        
    }];
    
    
}


-(void)DeleAllHistory
{
    [Quedb inDatabase:^(FMDatabase * _Nonnull db) {
        
        BOOL success=[db executeUpdate:@"DELETE FROM t_searchhistory"];
        
        if (success) {
            NSLog(@"删除成功");
        } else {
            NSLog(@"删除失败");
        }
        
    }];
    
}

-(NSArray*)QueryData
{
    
    NSMutableArray *SearchArr=[NSMutableArray array];
    
    [Quedb inDatabase:^(FMDatabase * _Nonnull db) {
        
        FMResultSet *result =[db executeQuery:@"SELECT * FROM t_searchhistory ORDER BY searchdate DESC LIMIT 10"];
        
        while ([result next]) {
            HistoryModel *model=[HistoryModel new];
            
            model.searchkey=[result stringForColumn:@"searchkey"];
            
            model.searchdate=[result stringForColumn:@"searchdate"];
            
            [SearchArr addObject:model];
        }
        
    }];
    
    return SearchArr;
}

-(NSArray*)queryAllData
{
    
    NSMutableArray *SearchArr=[NSMutableArray array];
    
    [Quedb inDatabase:^(FMDatabase * _Nonnull db) {
        
        FMResultSet *result =[db executeQuery:@"SELECT * FROM t_searchhistory ORDER BY searchdate DESC"];
        
        while ([result next]) {
            HistoryModel *model=[HistoryModel new];
            
            model.searchkey=[result stringForColumn:@"searchkey"];
            
            model.searchdate=[result stringForColumn:@"searchdate"];
            
            [SearchArr addObject:model];
        }
        
    }];
    
    return SearchArr;
    
}

@end
